package com.tanjun.mallservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.common.exception.BizException;
import com.tanjun.mallservice.web.orm.entity.MallPluSku;
import com.tanjun.mallservice.web.orm.entity.MallShopCar;
import com.tanjun.mallservice.web.orm.mapper.MallShopCarMapper;
import com.tanjun.mallservice.web.service.MallPluSkuService;
import com.tanjun.mallservice.web.service.MallShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author jjtan
 * @description 针对表【mall_shop_car(购物车)】的数据库操作Service实现
 * @createDate 2024-04-01 15:46:19
 */
@Service
public class MallShopCarServiceImpl extends ServiceImpl<MallShopCarMapper, MallShopCar>
        implements MallShopCarService {

    @Autowired
    private MallPluSkuService mallPluSkuService;



    /**
     * 设置购物车商品数量
     *
     * @param userId 用户ID
     * @param pluId  商品Id
     * @param skuId  规格Id
     * @param skuNum 数量
     */
    @Override
    @CacheEvict(value = {"mallShopCarService","countShopCarPluSku"},key = "#userId")
    public void setShopCarNum(Long userId, Long pluId, Long skuId, Integer skuNum, Boolean isAdd) {
        MallPluSku sku = mallPluSkuService.getSkuById(skuId);
        if (sku == null) {
            throw new BizException("规格不存在");
        }
        if (!sku.getSkuEnable()) {
            throw new BizException("规格不可用");
        }
        if (!Objects.equals(pluId, sku.getPluId())) {
            throw new BizException("商品规格不匹配");
        }
        MallShopCar old = this.list(QueryChain.of(MallShopCar.class)
                .eq(MallShopCar::getUserId, userId)
                .eq(MallShopCar::getSkuId, skuId)
        ).stream().findFirst().orElse(null);
        if (old != null) {
               UpdateChain.of(MallShopCar.class)
                        .set(MallShopCar::getSkuNum, isAdd ? old.getSkuNum() + skuNum : skuNum)
                        .set(MallShopCar::getAddSkuPrice, sku.getSkuPrice()) //增加数量的时候，将价格改为最新的价格
                        .eq(MallShopCar::getShopCarId, old.getShopCarId())
                       .update()
                ;

        } else { //不存在记录
            MallShopCar shopCar = new MallShopCar();
            shopCar.setPluId(pluId);
            shopCar.setSkuId(skuId);
            shopCar.setUserId(userId);
            shopCar.setSkuNum(skuNum);
            shopCar.setAddSkuPrice(sku.getSkuPrice());
            this.save(shopCar);
        }
    }

    @Override
    @Cacheable(value = "mallShopCarService",key = "#userId")
    public List<MallShopCar> listShopCarPluSku(Long userId) {
        return this.list(QueryChain.of(MallShopCar.class)
                .eq(MallShopCar::getUserId,userId)
                .orderBy(MallShopCar::getShopCarId)
                .asc()
        );
    }

    @Override
    @Cacheable(value = "countShopCarPluSku",key = "#userId")
    public long countShopCarPluSku(Long userId) {
        return this.count(QueryChain.of(MallShopCar.class)
                .eq(MallShopCar::getUserId,userId)
        );
    }

    @Override
    @CacheEvict(value = {"mallShopCarService", "countShopCarPluSku"}, key = "#userId")
    public void deleteShopCar(Long userId, List<Long> shopCarIdList) {
        this.remove(QueryChain.of(MallShopCar.class)
                .eq(MallShopCar::getUserId, userId)
                .in(MallShopCar::getShopCarId, shopCarIdList)
        );
    }

}




